//
//  BCPayPassOverlayView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/12/11.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCPayPassOverlayView.h"


@interface BCPayPassOverlayView ()

@property (nonatomic, strong) CAShapeLayer *sepLayer;

/**
 *passwordLayer
 */
@property (nonatomic, strong) CAShapeLayer *passwordLayer;

@end

@implementation BCPayPassOverlayView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.layer.cornerRadius = 2;
        self.layer.borderColor = [UIColor qmui_colorWithHexString:@"#FFC2C2C2"].CGColor;
        self.layer.borderWidth = 2;
        self.layer.masksToBounds = false;
        //
        [self setupLayers];
    }
    return self;
}
- (void)setupLayers {
    self.sepLayer = [CAShapeLayer layer];
    self.sepLayer.strokeColor = self.layer.borderColor;
    self.sepLayer.lineWidth = self.layer.borderWidth;
    self.sepLayer.fillColor = [UIColor clearColor].CGColor;
    [self.layer addSublayer:self.sepLayer];
    //
    self.passwordLayer = [CAShapeLayer layer];
    self.passwordLayer.strokeColor = [UIColor clearColor].CGColor;
    self.passwordLayer.fillColor = [UIColor qmui_colorWithHexString:@"#333333"].CGColor;
    [self.layer addSublayer:self.passwordLayer];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    self.sepLayer.frame = self.bounds;
    [self setSepLayerPath];
    //
    self.passwordLayer.frame = self.bounds;
}
- (void)setSepLayerPath {
    CGFloat width = CGRectGetWidth(self.frame);
    CGFloat height = CGRectGetHeight(self.frame);
    //
    UIBezierPath *bPath = [UIBezierPath bezierPath];
    CGFloat padding = width/6;
    for (int i=0; i<5; i++) {
        CGPoint startPoint = CGPointMake(padding+i*padding-1.0, 0);
        CGPoint endPoint = CGPointMake(padding+i*padding-1.0, height);
        [bPath moveToPoint:startPoint];
        [bPath addLineToPoint:endPoint];
    }
    self.sepLayer.path = bPath.CGPath;
}

- (void)updatePasswordNumber:(NSInteger)number {
    if (number<=0) {
        self.passwordLayer.path = nil;
    }else {
        UIBezierPath *bPath = [UIBezierPath bezierPath];
        CGFloat width = CGRectGetWidth(self.frame);
        CGFloat height = CGRectGetHeight(self.frame);
        CGFloat radius = 6.0;
        CGFloat padding = width/6;
        for (int i=0; i<number; i++) {
            CGPoint center = CGPointMake(padding/2+i*padding, height/2);
            [bPath moveToPoint:center];
            [bPath addArcWithCenter:center radius:radius startAngle:0 endAngle:M_PI*2 clockwise:true];
        }
        self.passwordLayer.path = bPath.CGPath;
    }
}


- (void)setTextField:(UITextField *)textField {
    _textField = textField;
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChangedNotification:) name:UITextFieldTextDidChangeNotification object:textField];
}

#pragma mark - textFieldTextDidChangedNotification:
- (void)textFieldTextDidChangedNotification:(NSNotification *)noti {
    if (noti.object==self.textField) {
        UITextField *textField = self.textField;
        [textField becomeFirstResponder];
        NSInteger length = textField.text.length;
        [self updatePasswordNumber:length];
    }
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:self.textField];
}

@end
